home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / distutils / command / config.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  12.5 KB  |  353 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''distutils.command.config
  5.  
  6. Implements the Distutils \'config\' command, a (mostly) empty command class
  7. that exists mainly to be sub-classed by specific module distributions and
  8. applications.  The idea is that while every "config" command is different,
  9. at least they\'re all named the same, and users always see "config" in the
  10. list of standard commands.  Also, this is a good place to put common
  11. configure-like tasks: "try to compile this C code", or "figure out where
  12. this header file lives".
  13. '''
  14. __revision__ = '$Id: config.py 37828 2004-11-10 22:23:15Z loewis $'
  15. import sys
  16. import os
  17. import string
  18. import re
  19. from types import *
  20. from distutils.core import Command
  21. from distutils.errors import DistutilsExecError
  22. from distutils.sysconfig import customize_compiler
  23. from distutils import log
  24. LANG_EXT = {
  25.     'c': '.c',
  26.     'c++': '.cxx' }
  27.  
  28. class config(Command):
  29.     description = 'prepare to build'
  30.     user_options = [
  31.         ('compiler=', None, 'specify the compiler type'),
  32.         ('cc=', None, 'specify the compiler executable'),
  33.         ('include-dirs=', 'I', 'list of directories to search for header files'),
  34.         ('define=', 'D', 'C preprocessor macros to define'),
  35.         ('undef=', 'U', 'C preprocessor macros to undefine'),
  36.         ('libraries=', 'l', 'external C libraries to link with'),
  37.         ('library-dirs=', 'L', 'directories to search for external C libraries'),
  38.         ('noisy', None, 'show every action (compile, link, run, ...) taken'),
  39.         ('dump-source', None, 'dump generated source files before attempting to compile them')]
  40.     
  41.     def initialize_options(self):
  42.         self.compiler = None
  43.         self.cc = None
  44.         self.include_dirs = None
  45.         self.libraries = None
  46.         self.library_dirs = None
  47.         self.noisy = 1
  48.         self.dump_source = 1
  49.         self.temp_files = []
  50.  
  51.     
  52.     def finalize_options(self):
  53.         if self.include_dirs is None:
  54.             if not self.distribution.include_dirs:
  55.                 pass
  56.             self.include_dirs = []
  57.         elif type(self.include_dirs) is StringType:
  58.             self.include_dirs = string.split(self.include_dirs, os.pathsep)
  59.         
  60.         if self.libraries is None:
  61.             self.libraries = []
  62.         elif type(self.libraries) is StringType:
  63.             self.libraries = [
  64.                 self.libraries]
  65.         
  66.         if self.library_dirs is None:
  67.             self.library_dirs = []
  68.         elif type(self.library_dirs) is StringType:
  69.             self.library_dirs = string.split(self.library_dirs, os.pathsep)
  70.         
  71.  
  72.     
  73.     def run(self):
  74.         pass
  75.  
  76.     
  77.     def _check_compiler(self):
  78.         """Check that 'self.compiler' really is a CCompiler object;
  79.         if not, make it one.
  80.         """
  81.         CCompiler = CCompiler
  82.         new_compiler = new_compiler
  83.         import distutils.ccompiler
  84.         if not isinstance(self.compiler, CCompiler):
  85.             self.compiler = new_compiler(compiler = self.compiler, dry_run = self.dry_run, force = 1)
  86.             customize_compiler(self.compiler)
  87.             if self.include_dirs:
  88.                 self.compiler.set_include_dirs(self.include_dirs)
  89.             
  90.             if self.libraries:
  91.                 self.compiler.set_libraries(self.libraries)
  92.             
  93.             if self.library_dirs:
  94.                 self.compiler.set_library_dirs(self.library_dirs)
  95.             
  96.         
  97.  
  98.     
  99.     def _gen_temp_sourcefile(self, body, headers, lang):
  100.         filename = '_configtest' + LANG_EXT[lang]
  101.         file = open(filename, 'w')
  102.         if headers:
  103.             for header in headers:
  104.                 file.write('#include <%s>\n' % header)
  105.             
  106.             file.write('\n')
  107.         
  108.         file.write(body)
  109.         if body[-1] != '\n':
  110.             file.write('\n')
  111.         
  112.         file.close()
  113.         return filename
  114.  
  115.     
  116.     def _preprocess(self, body, headers, include_dirs, lang):
  117.         src = self._gen_temp_sourcefile(body, headers, lang)
  118.         out = '_configtest.i'
  119.         self.temp_files.extend([
  120.             src,
  121.             out])
  122.         self.compiler.preprocess(src, out, include_dirs = include_dirs)
  123.         return (src, out)
  124.  
  125.     
  126.     def _compile(self, body, headers, include_dirs, lang):
  127.         src = self._gen_temp_sourcefile(body, headers, lang)
  128.         if self.dump_source:
  129.             dump_file(src, "compiling '%s':" % src)
  130.         
  131.         (obj,) = self.compiler.object_filenames([
  132.             src])
  133.         self.temp_files.extend([
  134.             src,
  135.             obj])
  136.         self.compiler.compile([
  137.             src], include_dirs = include_dirs)
  138.         return (src, obj)
  139.  
  140.     
  141.     def _link(self, body, headers, include_dirs, libraries, library_dirs, lang):
  142.         (src, obj) = self._compile(body, headers, include_dirs, lang)
  143.         prog = os.path.splitext(os.path.basename(src))[0]
  144.         self.compiler.link_executable([
  145.             obj], prog, libraries = libraries, library_dirs = library_dirs, target_lang = lang)
  146.         if self.compiler.exe_extension is not None:
  147.             prog = prog + self.compiler.exe_extension
  148.         
  149.         self.temp_files.append(prog)
  150.         return (src, obj, prog)
  151.  
  152.     
  153.     def _clean(self, *filenames):
  154.         if not filenames:
  155.             filenames = self.temp_files
  156.             self.temp_files = []
  157.         
  158.         log.info('removing: %s', string.join(filenames))
  159.         for filename in filenames:
  160.             
  161.             try:
  162.                 os.remove(filename)
  163.             continue
  164.             except OSError:
  165.                 continue
  166.             
  167.  
  168.         
  169.  
  170.     
  171.     def try_cpp(self, body = None, headers = None, include_dirs = None, lang = 'c'):
  172.         """Construct a source file from 'body' (a string containing lines
  173.         of C/C++ code) and 'headers' (a list of header files to include)
  174.         and run it through the preprocessor.  Return true if the
  175.         preprocessor succeeded, false if there were any errors.
  176.         ('body' probably isn't of much use, but what the heck.)
  177.         """
  178.         CompileError = CompileError
  179.         import distutils.ccompiler
  180.         self._check_compiler()
  181.         ok = 1
  182.         
  183.         try:
  184.             self._preprocess(body, headers, include_dirs, lang)
  185.         except CompileError:
  186.             ok = 0
  187.  
  188.         self._clean()
  189.         return ok
  190.  
  191.     
  192.     def search_cpp(self, pattern, body = None, headers = None, include_dirs = None, lang = 'c'):
  193.         """Construct a source file (just like 'try_cpp()'), run it through
  194.         the preprocessor, and return true if any line of the output matches
  195.         'pattern'.  'pattern' should either be a compiled regex object or a
  196.         string containing a regex.  If both 'body' and 'headers' are None,
  197.         preprocesses an empty file -- which can be useful to determine the
  198.         symbols the preprocessor and compiler set by default.
  199.         """
  200.         self._check_compiler()
  201.         (src, out) = self._preprocess(body, headers, include_dirs, lang)
  202.         if type(pattern) is StringType:
  203.             pattern = re.compile(pattern)
  204.         
  205.         file = open(out)
  206.         match = 0
  207.         while None:
  208.             line = file.readline()
  209.             if line == '':
  210.                 break
  211.             
  212.             if pattern.search(line):
  213.                 match = 1
  214.                 break
  215.                 continue
  216.             continue
  217.             file.close()
  218.             self._clean()
  219.             return match
  220.  
  221.     
  222.     def try_compile(self, body, headers = None, include_dirs = None, lang = 'c'):
  223.         """Try to compile a source file built from 'body' and 'headers'.
  224.         Return true on success, false otherwise.
  225.         """
  226.         CompileError = CompileError
  227.         import distutils.ccompiler
  228.         self._check_compiler()
  229.         
  230.         try:
  231.             self._compile(body, headers, include_dirs, lang)
  232.             ok = 1
  233.         except CompileError:
  234.             ok = 0
  235.  
  236.         if not ok or 'success!':
  237.             pass
  238.         log.info('failure.')
  239.         self._clean()
  240.         return ok
  241.  
  242.     
  243.     def try_link(self, body, headers = None, include_dirs = None, libraries = None, library_dirs = None, lang = 'c'):
  244.         """Try to compile and link a source file, built from 'body' and
  245.         'headers', to executable form.  Return true on success, false
  246.         otherwise.
  247.         """
  248.         CompileError = CompileError
  249.         LinkError = LinkError
  250.         import distutils.ccompiler
  251.         self._check_compiler()
  252.         
  253.         try:
  254.             self._link(body, headers, include_dirs, libraries, library_dirs, lang)
  255.             ok = 1
  256.         except (CompileError, LinkError):
  257.             ok = 0
  258.  
  259.         if not ok or 'success!':
  260.             pass
  261.         log.info('failure.')
  262.         self._clean()
  263.         return ok
  264.  
  265.     
  266.     def try_run(self, body, headers = None, include_dirs = None, libraries = None, library_dirs = None, lang = 'c'):
  267.         """Try to compile, link to an executable, and run a program
  268.         built from 'body' and 'headers'.  Return true on success, false
  269.         otherwise.
  270.         """
  271.         CompileError = CompileError
  272.         LinkError = LinkError
  273.         import distutils.ccompiler
  274.         self._check_compiler()
  275.         
  276.         try:
  277.             (src, obj, exe) = self._link(body, headers, include_dirs, libraries, library_dirs, lang)
  278.             self.spawn([
  279.                 exe])
  280.             ok = 1
  281.         except (CompileError, LinkError, DistutilsExecError):
  282.             ok = 0
  283.  
  284.         if not ok or 'success!':
  285.             pass
  286.         log.info('failure.')
  287.         self._clean()
  288.         return ok
  289.  
  290.     
  291.     def check_func(self, func, headers = None, include_dirs = None, libraries = None, library_dirs = None, decl = 0, call = 0):
  292.         '''Determine if function \'func\' is available by constructing a
  293.         source file that refers to \'func\', and compiles and links it.
  294.         If everything succeeds, returns true; otherwise returns false.
  295.  
  296.         The constructed source file starts out by including the header
  297.         files listed in \'headers\'.  If \'decl\' is true, it then declares
  298.         \'func\' (as "int func()"); you probably shouldn\'t supply \'headers\'
  299.         and set \'decl\' true in the same call, or you might get errors about
  300.         a conflicting declarations for \'func\'.  Finally, the constructed
  301.         \'main()\' function either references \'func\' or (if \'call\' is true)
  302.         calls it.  \'libraries\' and \'library_dirs\' are used when
  303.         linking.
  304.         '''
  305.         self._check_compiler()
  306.         body = []
  307.         if decl:
  308.             body.append('int %s ();' % func)
  309.         
  310.         body.append('int main () {')
  311.         if call:
  312.             body.append('  %s();' % func)
  313.         else:
  314.             body.append('  %s;' % func)
  315.         body.append('}')
  316.         body = string.join(body, '\n') + '\n'
  317.         return self.try_link(body, headers, include_dirs, libraries, library_dirs)
  318.  
  319.     
  320.     def check_lib(self, library, library_dirs = None, headers = None, include_dirs = None, other_libraries = []):
  321.         """Determine if 'library' is available to be linked against,
  322.         without actually checking that any particular symbols are provided
  323.         by it.  'headers' will be used in constructing the source file to
  324.         be compiled, but the only effect of this is to check if all the
  325.         header files listed are available.  Any libraries listed in
  326.         'other_libraries' will be included in the link, in case 'library'
  327.         has symbols that depend on other libraries.
  328.         """
  329.         self._check_compiler()
  330.         return self.try_link('int main (void) { }', headers, include_dirs, [
  331.             library] + other_libraries, library_dirs)
  332.  
  333.     
  334.     def check_header(self, header, include_dirs = None, library_dirs = None, lang = 'c'):
  335.         """Determine if the system header file named by 'header_file'
  336.         exists and can be found by the preprocessor; return true if so,
  337.         false otherwise.
  338.         """
  339.         return self.try_cpp(body = '/* No body */', headers = [
  340.             header], include_dirs = include_dirs)
  341.  
  342.  
  343.  
  344. def dump_file(filename, head = None):
  345.     if head is None:
  346.         print filename + ':'
  347.     else:
  348.         print head
  349.     file = open(filename)
  350.     sys.stdout.write(file.read())
  351.     file.close()
  352.  
  353.